package com.java.basis.sort;

import java.util.Arrays;

public class SelectionSort {
    public static void main(String[] args) {
//        int[] a = {5, 9, 7, 4, 1, 3, 2, 8};
//        int[] a = {1, 2, 3, 4, 5, 7, 8, 9};
        int[] a = {5, 3, 7, 2, 1, 9, 8, 4};
        selection(a);


    }

    /**
     * 优化后的选择排序，等每一轮结束后再交换对应的值
     *
     */
    private static void selection(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            //i代表每轮选择最小元素要交换到的目标索引
            int s = i;//代表最小元素的索引
            for (int j = s + 1; j < a.length; j++) {
                System.out.println("交换次数");
                if (a[s] > a[j]) {
                    s = j;
                }
            }
            if (s != i) {
                BubbleSort.swap(a, s, i);
            }
            System.out.println("每轮选择排序输出" + Arrays.toString(a));

        }
    }
}
